package com.lamda.zhekou.dao.impl;

import com.lamda.zhekou.dao.VpsBuyRecordDao;
import com.lamda.zhekou.model.VPS;
import com.lamda.zhekou.model.VpsBuyRecord;
import com.lamda.zhekou.vo.IncomeTotalVo;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import javax.management.Query;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by MJiao on 15/8/27.
 */
@Repository
public class VpsBuyRecordDaoImpl extends GenericDaoImpl<VpsBuyRecord> implements VpsBuyRecordDao {
    @Override
    public List<IncomeTotalVo> getIncomeTotalList() {
        String qlString = "select e.customer.nickname, sum(e.rmb) from VpsBuyRecord e group by e.customer.id";
        List resultList = this.entityManager.createQuery(qlString).getResultList();
        List<IncomeTotalVo> incomeTotalVoList = new ArrayList<>();
        for (Object o : resultList) {
            Object[] array = (Object[]) o;

            IncomeTotalVo incomeTotalVo = new IncomeTotalVo();
            incomeTotalVo.setNickname((String) array[0]);
            incomeTotalVo.setRmb(Float.parseFloat(array[1].toString()));
            incomeTotalVoList.add(incomeTotalVo);
        }
        return incomeTotalVoList;
    }

    @Override
    public Double getIncomeTotal() {
        String qlString = "select sum(e.rmb) from VpsBuyRecord e";
        List<Double> list = this.entityManager.createQuery(qlString).getResultList();
        return list.get(0);
    }

    @Override
    public List<VpsBuyRecord> findVpsBuyRecords(Set<VPS> vpsSet, Timestamp startTimestamp, Timestamp endTimestamp) {
        String qlString = "select e from VpsBuyRecord e where e.createTime > :startTimestamp and e.createTime < :endTimestamp and e.vps in :inclList";
        return this.entityManager.createQuery(qlString).setParameter("startTimestamp", startTimestamp)
                .setParameter("endTimestamp", endTimestamp)
                .setParameter("inclList", vpsSet)
                .getResultList();
    }

    @Override
    public List<IncomeTotalVo> getIncomeTotalList(Timestamp startTimestamp, Timestamp endTimestamp) {
        String qlString = "select e.customer.nickname, sum(e.rmb) from VpsBuyRecord e where e.createTime > :startTimestamp and e.createTime < :endTimestamp  group by e.customer.id";
        List resultList = this.entityManager.createQuery(qlString)
                .setParameter("startTimestamp", startTimestamp)
                .setParameter("endTimestamp", endTimestamp)
                .getResultList();
        List<IncomeTotalVo> incomeTotalVoList = new ArrayList<>();
        for (Object o : resultList) {
            Object[] array = (Object[]) o;

            IncomeTotalVo incomeTotalVo = new IncomeTotalVo();
            incomeTotalVo.setNickname((String) array[0]);
            incomeTotalVo.setRmb(Float.parseFloat(array[1].toString()));
            incomeTotalVoList.add(incomeTotalVo);
        }
        return incomeTotalVoList;
    }


    @Override
    public List<IncomeTotalVo> findIncomeTotalList(Set<VPS> vpsSet, Timestamp startTimestamp, Timestamp endTimestamp) {
        String qlString = "select e.seller.nickname, sum(e.rmb) from VpsBuyRecord e where e.createTime > :startTimestamp and e.createTime < :endTimestamp  and e.vps in :inclList group by e.seller.id";
        List resultList = this.entityManager.createQuery(qlString)
                .setParameter("startTimestamp", startTimestamp)
                .setParameter("endTimestamp", endTimestamp)
                .setParameter("inclList", vpsSet)
                .getResultList();
        List<IncomeTotalVo> incomeTotalVoList = new ArrayList<>();
        for (Object o : resultList) {
            Object[] array = (Object[]) o;

            IncomeTotalVo incomeTotalVo = new IncomeTotalVo();
            incomeTotalVo.setNickname((String) array[0]);
            incomeTotalVo.setRmb(Float.parseFloat(array[1].toString()));
            incomeTotalVoList.add(incomeTotalVo);
        }
        return incomeTotalVoList;
    }

    /**
     * 统计vps的
     *
     * @param vpsSet
     * @param startTimestamp
     * @param endTimestamp
     * @return
     */
    @Override
    public List<VpsBuyRecord> findVpsSellRecords(Set<VPS> vpsSet, Timestamp startTimestamp, Timestamp endTimestamp) {
        return null;
    }


}
